<?php
namespace hotel\modules\apiadmin\controllers;
use hotel\common\models\Hotel;
use hotel\common\models\RoomType;
use hotel\common\models\RoomTypeActivity;
use hotel\common\models\RoomTypeActivityOff;
use hotel\common\models\RoomTypeOff;
use hotel\common\utils\HotelServices;
use hotel\common\utils\Tool;
use hotel\modules\apiadmin\models\RoomFacilitiesType;
use \Yii;

class RoomTypeController extends CoreController
{
	//获取房型信息
	public function actionGetRoomTypeData(){
		$page_size = $this->request('page_size',10);
		$page = $this->request('page',1);
		$list = RoomType::find()->where("appid = '$this->appid'")->offset($page_size*($page-1))->limit($page_size)->asArray()->all();
		$counts = RoomType::find()->count();
		$data['list'] = $list;
		$data['pageInfo'] = [
			'count' => $counts,
			'pageSize' => $page_size,
			'currPage' => $page,
			'tatalPage' => ceil($counts/$page_size),
		];
		return Tool::showRes([0, '获取房型列表成功'],$data);
	}

	//同步房型信息
	public function actionRoomTypeTongBu(){
        $session = HotelServices::checkXopSession($this->appid);
		$res = HotelServices::xopgetroomtype($session,$this->appid);
		$room_type_data = $res['results'];
		if(empty($room_type_data)){
			return Tool::showRes([0, '暂无数据']);
		}
		$rmtype_arr = [];
		foreach($room_type_data as $k=>$v){
			$rmtype = $v['rmtype'];
			$des = $v['des'];
			$temp = RoomType::find()->where("appid = '$this->appid' and rmtype = '$rmtype'")->asArray()->one();
			if($temp){
				$model = RoomType::findOne($temp['id']);
				$model->des = $des;
				$model->create_time = date('Y-m-d H:i:s');
			}else{
				$model = New RoomType();
				$model->des = $des;
				$model->appid = $this->appid;
				$model->rmtype = $rmtype;
				$model->create_time = date('Y-m-d H:i:s');
			}
			$model->save();
			array_push($rmtype_arr,$rmtype);
		}
		//删除西软没有的房型
		RoomType::deleteAll(['AND', 'appid = :appid', ['NOT IN', 'rmtype', $rmtype_arr]], [':appid' => $this->appid]);
		return Tool::showRes([0, '同步成功']);
	}


	//根据id获取房型信息
	public function actionGetRoomTypeDetail(){
		$id = $this->request('id');
		if(empty($id)){
			return Tool::showRes([400, 'id参数不能为空']);
		}
		$data = RoomType::find()->where("id = $id")->asArray()->one();
		$room_facilities = json_decode($data['room_facilities'],true);
		if(!empty($room_facilities)){
			foreach($room_facilities as $k=>$v){
				$id = $v['id'];
				$res = RoomFacilitiesType::find()->where("id = $id")->asArray()->one();
				$room_facilities[$k]['icon_url'] = $res['icon_url'];
			}
			$data['room_facilities'] = $room_facilities;
		}else{
			$data['room_facilities'] = [];
		}

		return Tool::showRes([0, '获取房型信息成功'],$data);
	}


	//编辑房型信息
	public function actionEditRoomTypeData(){

		$id = $this->request('id');
		if(empty($id)){
			return Tool::showRes([400, 'id不能为空']);
		}
		$model = new RoomType();
		$model = $model::findOne($id);
		$model->cover_image_url = $this->request('cover_image_url');
		$model->banners = $this->request('banners');
		$model->description = $this->request('description');
		$model->reservation_rules = $this->request('reservation_rules');
		$model->room_facilities = $this->request('room_facilities');
		$model->label = $this->request('label');
		$model->is_place_order = $this->request('is_place_order');
		$model->is_show = $this->request('is_show');
		$model->des = $this->request('des');
		$model->food = $this->request('food');
		$model->entertainment = $this->request('entertainment');
		$model->refund = $this->request('refund');
		$model->gift = $this->request('gift');
		$model->original_price = $this->request('original_price');
        $model->ticket_detail = $this->request('ticket_detail');
        $model->discount_label = $this->request('discount_label');
		if($model->save()) {
			return Tool::showRes([0, '操作成功']);
		}
		return Tool::showRes([400, '操作失败']);
	}

//设置下架按钮
	public function actionSetRoomTypeOff(){
		$date_arr = $this->request('date');
		$type = $this->request('type');
		$room_type_id = $this->request('room_type_id');
		if(empty($date_arr) || empty($type) || empty($room_type_id)){
			return Tool::showRes([400, '参数有误']);
		}
		if($type == 1){
			$arr = [];
			foreach($date_arr as $k=>$v){
				$arr[$k][] = $room_type_id;
				$arr[$k][] = $v;
				$arr[$k][] = date('Y-m-d H:i:s');
			}
			$res= \Yii::$app->db->createCommand()->batchInsert(RoomTypeOff::tableName(), ['room_type_id','date','create_time'], $arr)->execute();
			if($res){
				return Tool::showRes([0, '操作成功']);
			}
			return Tool::showRes([400, '操作失败']);
		}else{
			if (false !== $res = RoomTypeOff::deleteAll(['in', 'date', $date_arr])) {
				return Tool::showRes([0, '操作成功']);
			} else {
				return Tool::showRes([400, '操作失败']);
			}
		}
	}

	//获取房型下架信息
	public function actionGetRoomTypeOff(){
		$room_type_id = $this->request('room_type_id');
		$data = RoomType::find()
			->with(['room'])
			->where("id = $room_type_id")
			->asArray()->all();
		return Tool::showRes([0, '获取房型信息成功'],$data);
	}


	//设置天天特惠
	public function actionSetRoomTypeActivityOff(){
		$date_arr = $this->request('date');
		$type = $this->request('type');
		$discount_type = $this->request('discount_type');
		$num = $this->request('num');
		$label = $this->request('label');
		$room_type_id = $this->request('room_type_id');
		if(empty($discount_type) || empty($type) || empty($room_type_id) || empty($num) ){
			return Tool::showRes([400, '参数有误']);
		}
		$res = RoomTypeActivity::find()->where("room_type_id = $room_type_id")->asArray()->one();
		if($res){
			$model = RoomTypeActivity::findOne($res['id']);
			$model->room_type_id = $room_type_id;
			$model->type = $discount_type;
			$model->num = $num;
			$model->label = $label;
			$model->save();
		}else{
			$model = New RoomTypeActivity();
			$model->room_type_id = $room_type_id;
			$model->type = $discount_type;
			$model->num = $num;
			$model->label = $label;
			$model->save();
		}

		if (!empty($date_arr)) {
			if ($type == 1) {
				$arr = [];
				foreach ($date_arr as $k => $v) {
					$arr[$k][] = $room_type_id;
					$arr[$k][] = $v;
					$arr[$k][] = date('Y-m-d H:i:s');
				}
				$res = \Yii::$app->db->createCommand()->batchInsert(RoomTypeActivityOff::tableName(), ['room_type_id', 'date', 'create_time'], $arr)->execute();
				if ($res) {
					return Tool::showRes([0, '操作成功']);
				}
				return Tool::showRes([400, '操作失败']);
			} else {
				if (false !== $res = RoomTypeActivityOff::deleteAll(['in', 'date', $date_arr])) {
					return Tool::showRes([0, '操作成功']);
				} else {
					return Tool::showRes([400, '操作失败']);
				}
			}
		}
		return Tool::showRes([0, '操作成功']);
	}

	//获取天天特惠信息
	public function actionGetRoomTypeActivityOff(){
		$room_type_id = $this->request('room_type_id');
		$data = RoomType::find()
			->with(['activityoff'])
			->with(['activity'])
			->where("id = $room_type_id")
			->asArray()->all();
		return Tool::showRes([0, '获取房型信息成功'],$data);
	}
}